<template>
  <div class="login_centainer">
    <div class="login_box">
      <el-form ref="loginref" :model="loginForm" :rules="loginFormrules" label-width="0px">
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            prefix-icon="icontfont icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="icontfont icon-3702mima"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="name">
          <el-input
            type="name"
            v-model="loginForm.name"
            prefix-icon="icontfont icon-user"
            placeholder="请输入真实姓名"
          ></el-input>

        </el-form-item>
        <el-form-item prop="code">
          <el-input
            type="text"
            v-model="loginForm.code"
            class="identifyinput"
            placeholder="- - - -"
          >
            <template slot="prepend">验证码</template>
            <template slot="append">
              <div class="login-code" @click="refreshCode">
                <Identify :identifyCode="identifyCode"></Identify>
              </div>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <el-button type="stuprimary" v-on:click="stulogin">学生注册</el-button>
      <el-button type="teacprimary" v-on:click="teaclogin">老师注册</el-button>
      <el-button type="adminprimary" v-on:click="adminlogin">管理员注册</el-button>
    </div>
  </div>
</template>

<script>
import { setStore } from "../libs/storage";
import { getStore } from "../libs/storage";
import Identify from "../util/identify.vue";

//import {setCookie} from "../libs/storage";
export default {
  name: "Login",
  components: { Identify },
  data() {
    const validateVerifycode = (rule, value, callback) => {
      var _self = this;
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else if (value !== _self.identifyCode) {
        console.log("validateVerifycode:", value);
        callback(new Error("验证码不正确!"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        name:"",
        code: "",
      
      },
      
      identifyCodes: "1234567890abcdefjhijklinopqrsduvwxyz",
      identifyCode: "",
      responseResult: [],
      loginFormrules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 5 到 15 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ],
        code: [
          { required: true, trigger: "blur", validator: validateVerifycode }
        ]
      }
    };
  },
  mounted() {
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
          this.randomNum(0, this.identifyCodes.length)
        ];
      }
      console.log(this.identifyCode);
    },
    stulogin() {
      this.$refs.loginref.validate(valid => {
        console.log(this.loginForm.username);
        console.log(this.loginForm.password);
        console.log(valid);
        if (!valid) return this.$message.info("登录失败");
        this.$axios
          .post("/sturegister", {
            stuAccount: this.loginForm.username,
            stuPass: this.loginForm.password,
            stuName:this.loginForm.name
          })
          .then(successResponse => {
            if (successResponse.data.code !== 200) {
              return this.$message.info("用户名或者密码错误");
            }
            setStore("userid", successResponse.data.data.userid);
            console.log(getStore("userid"));
            console.log(successResponse);
            window.sessionStorage.setItem(
              "token",
              successResponse.data.data.userid
            );
            this.$router.replace({ path: "/" });
          })
          .catch(failResponse => {
            console.log(failResponse.responseResult);
          });
      });
    },
    teaclogin() {
      this.$refs.loginref.validate(valid => {
        console.log(this.loginForm.username);
        console.log(this.loginForm.password);
        console.log(valid);
        if (!valid) return this.$message.info("登录失败");
        this.$axios
          .post("/teacregister", {
            teacAccount: this.loginForm.username,
            teacPass: this.loginForm.password,
            teachername:this.loginForm.name
          })
          .then(successResponse => {
            if (successResponse.data.code !== 200) {
              return this.$message.info("用户名或者密码错误");
            }
            setStore("userid", successResponse.data.data.userid);
            console.log(getStore("userid"));
            console.log(successResponse);
            window.sessionStorage.setItem(
              "token",
              successResponse.data.data.userid
            );
            this.$router.replace({ path: "/" });
          })
          .catch(failResponse => {
            console.log(failResponse.responseResult);
          });
      });
    },
    adminlogin() {
      this.$refs.loginref.validate(valid => {
        console.log(this.loginForm.username);
        console.log(this.loginForm.password);
        console.log(valid);
        if (!valid) return this.$message.info("登录失败");
        var that=this;
        this.$axios
          .post("/adminregister", {
            admAccount: this.loginForm.username,
            admPass: this.loginForm.password
          })
          .then(successResponse => {
            if (successResponse.data.code !== 200) {
              return this.$message.info("用户名或者密码错误");
            }
            setStore("userid", successResponse.data.data.userid);
            console.log(getStore("userid"));
            console.log(successResponse);
            window.sessionStorage.setItem(
              "token",
              successResponse.data.data.userid
            );
            this.$router.replace({ path: "/" });
          })
          .catch(failResponse => {
            console.log(failResponse.responseResult);
          });
      });
    }
  }
};
</script>

<style scoped>
.login_centainer {
  background-image: url("../assets/back.png");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.identifybox {
  display: flex;
  justify-content: space-between;
  margin-top: 7px;
}
.login-code {
  height: 35px;
}
</style>
